package com.example.demo.common;

import sun.misc.BASE64Decoder;

/**
 * 将前端传来的截图base64转化为byte【】
 *
 * @author lai
 * @data 2021/12/8
 * @apiNote
 */
public class Face {

    private byte[] byt;

    public byte[] getByt(String base64Str) {
        // 解密
        BASE64Decoder decoder = new BASE64Decoder();
        // 去掉base64前缀 data:image/jpeg;base64,
        base64Str = base64Str.substring(base64Str.indexOf(",", 1) + 1, base64Str.length());
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(base64Str);
            for (int i = 0; i < bytes.length; ++i) {
                // 调整异常数据
                if (bytes[i] < 0) {
                    bytes[i] += 256;
                }
            }
            byt = bytes;
        } catch (Exception e) {
            System.out.println(e);
        }
        return byt;
    }

}
